<?php include 'header.php'; ?>
            <h2>最新帖子</h2>
            <?php
            include 'db_connect.php';

            $sql = "SELECT * FROM posts ORDER BY created_at DESC";
            $result = $conn->query($sql);

            if ($result->num_rows > 0) {
                while($row = $result->fetch_assoc()) {
                    echo '<article class="post">';
                    echo '<h2><a href="post.php?id=' . $row["id"] . '">' . $row["title"] . '</a></h2>';
                    echo '<div class="post-meta">作者: ' . $row["author"] . ' | 发布时间: ' . $row["created_at"] . '</div>';
                    echo '<p>' . substr($row["content"], 0, 200) . '...</p>';
                    echo '<a href="post.php?id=' . $row["id"] . '" class="btn">阅读全文</a>';
                    echo '</article>';
                }
            } else {
                echo '<p>暂无帖子，请发布第一个帖子！</p>';
            }
            $conn->close();
            ?>
<?php include 'footer.php'; ?>
